<?php
class Qltintuc extends CI_Controller {

    function __construct() {
        parent::__construct();
       
         if ($this->session->userdata("logged_in") != true) {
            header("location:./");
        }
         $this->load->Model("Mqltintuc","tintuc");
         $this->load->model('topic_model', 'topic');
          $this->load->model('category_model', 'category');  
    }
    
    public function index(){
       
      $this->viewAll();
    }
function viewAll($sort= 'topic_news_date',$order = 'DESC', $name = '0', $category = '0',$sub_category = '0', $status = '-1',$limit = '5', $start = '0')
    {
        $name = urldecode($name);
        $category = urldecode($category);
        $sub_category = urldecode($sub_category);
        $status = urldecode($status);
        $filter = array(
            'sort'=>$sort,
            'order' => $order,
            'name' => $name,
            'category' => $category,
            'sub_category' => $sub_category,
            'status' => $status,
            'start' => $start, 
            'limit' => $limit
        );
        $data['topics'] = $this->tintuc->listAll($filter);
		$totaltopic = $this->tintuc->totaltopic($filter);
		$data['totaltopic'] = $totaltopic;
        $data['name'] = $name;
        $data['sort'] = $sort;
        $data['category_filter'] = $category;
        $data['sub_category_filter'] = $sub_category;
        $data['status'] = $status;
        $data['start'] = $start;
        $data['limit'] = $limit;
        $data['order'] = $order;
		
		 // phân trang cho app
        $this->load->library('pagination');
        $config['base_url'] = site_url("qltintuc/viewAll/$sort/$order/$name/$category/$sub_category/$status/$limit");
        $config['total_rows'] = $totaltopic;
        $config['uri_segment'] = 10;
        $config['per_page'] =$limit;
        $config['full_tag_open'] = '<div class="pagination"><div class="links">';
        $config['full_tag_close'] = '</div></div>';
        $config['cur_tag_open'] = '<span class="active curved">';
        $config['cur_tag_close'] = '</span>';
        $config['num_tag_open'] = '';
        $config['num_tag_close'] = '';
        $config['next_link'] = 'Next';
        $config['prev_link'] = 'Previous';
        $config['prev_tag_open'] = '';
        $config['prev_tag_close'] = '';
        $config['last_tag_open'] = '';
        $config['last_tag_close'] = '';
        $this->pagination->initialize($config);
		
		if(isset($this->msg['error'])) $data['error'] = $this->msg['error'];
		if(isset($this->msg['success'])) $data['success'] = $this->msg['success'];
		$data['categorys'] = $this->category->listAll();
		$data['sub_categorys'] = $this->category->listSubCate();
	$ketqua = $this->tintuc->getoption();
        $this->load->helper('form');
        $data['topic'] = $ketqua;
          $data["js"] = Array('qltintuc.js','jquery.sortable.js','jquery.sortable.min.js','jquery-1.3.2.min.js','jquery-ui-1.7.1.custom.min.js'); 
         $data["username"] = $this->session->userdata('username');
        $this->load->view(__TEMPLATES_PATH__."/admin/qltintuc",$data);
	}
    public function suanumber(){
        foreach ($_POST['ketqua'] as $position => $item) :
            $sumposition = $position+1;
            $sql = "UPDATE `topic_news` SET `topic_news_number` = $sumposition WHERE `topic_news_id` = $item";
            $this->tintuc->updatequery($sql);
endforeach;
echo json_encode("Thanh cong");

    }
    public function xoanumber($id){
        $this->tintuc->xoaso($id);
        echo "<script>window.location='../'</script>";
        
    }

}